package com.buildings.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.bean.TaskBean;

import java.util.ArrayList;
import java.util.List;


/**
 * 任务列表
 */

public class TaskListAdapter extends RecyclerView.Adapter<TaskListAdapter.ViewHolder> {

    private List<TaskBean> data;
    private Activity activity;

    public TaskListAdapter(Activity activity, List<TaskBean> data) {
        this.data = data;
        this.activity = activity;
    }

    public void update(List<TaskBean> data) {
        this.data = data;
        this.notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task_list, parent, false);
        ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener != null){
                    listener.onItemClick(view,(Integer) view.getTag());
                }
            }
        });
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(listener != null){
                    listener.onItemLongClick(v,(Integer) v.getTag());
                }
                return false;
            }
        });
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int i) {
        TaskBean bean = data.get(i);
        holder.itemView.setTag(i);
        holder.tvName.setText(bean.getTaskName());
        holder.btnTask.setVisibility(View.VISIBLE);
        if(TextUtils.equals("2",bean.getStatus())){
            holder.btnTask.setVisibility(View.INVISIBLE);
            holder.tvStatus.setText("已完成");
            holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_finished));
        }else {
            holder.tvStatus.setText("未完成");
            holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_unfinished));
        }

        holder.btnTask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onTask(v,i);
            }
        });
        holder.btnForm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onForm(v,i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvStatus;//状态
        TextView tvName;//名称
        ImageView btnTask;//执行任务
        ImageView btnForm;//查看表单
        public ViewHolder(View itemView) {
            super(itemView);
            tvStatus = itemView.findViewById(R.id.tv_status);
            tvName = itemView.findViewById(R.id.tv_name);
            btnTask = itemView.findViewById(R.id.btn_task);
            btnForm = itemView.findViewById(R.id.btn_form);
        }
    }

    private OnItemClickListener listener;
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
        void onTask(View view, int position);
        void onForm(View view, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

}